import { uploadImg as img } from '@/api/system/upload'
import { message } from 'ant-design-vue'

/**
 * 上传图片
 * @param { File } file 文件对象
 * @param { string } fieldName 字段名
 * @param { object} form
 * @returns { Promise<string> }
 */
export function uploadImg(file, fieldName, form) {
  return new Promise((resolve, reject) => {
    form[fieldName + 'loading'] = true
    const formData = new FormData()
    formData.append('img', file)
    img(formData)
      .then((data) => {
        form[fieldName + 'loading'] = false
        resolve(data.path)
      })
      .catch((err) => {
        message.error(err)
        reject(err)
      })
  })
}
